package com.sc.web.quartz;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;

import com.sc.dal.dto.order.TCustomer;
import com.sc.dal.dto.order.TDataSource;
import com.sc.dal.dto.order.TInventory;
import com.sc.dal.dto.order.TSaleOrder;
import com.sc.dal.dto.order.TSaleOrderDetails;
import com.sc.dal.dto.order.TSaleOrderDto;
import com.sc.dal.dto.order.TSaleOrderInsertDto;
import com.sc.dal.dto.order.TSaleOrderInsertResult;
import com.sc.dal.dto.order.TSaleOrderResult;
import com.sc.dal.dto.order.TUnit;
import com.sc.dal.mapper.order.OrderDetailMapper;
import com.sc.dal.mapper.order.OrderShipMapper;
import com.sc.dal.model.goods.Goods;
import com.sc.dal.model.order.OrderDetail;
import com.sc.dal.model.order.OrderShip;
import com.sc.dal.model.order.Orders;
import com.sc.service.goods.GoodsService;
import com.sc.service.order.OrderService;
import com.sc.util.data.Common;
import com.sc.util.data.Constants;
import com.sc.util.openAPI.TOpenAPIUtil;

/**
 * @ClassName: SaleOrderBatchInsertTask
 * @Description:向T+系统批量增加销售订单
 * @author LeiJia
 * @date 2017年4月18日上午10:19
 * @version V1.0
 */
public class SaleOrderBatchInsertJob {

  //  private static Logger log = LoggerFactory.getLogger(SaleOrderBatchInsertJob.class);  
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private OrderShipMapper orderShipMapper;

	@Autowired
	private OrderDetailMapper orderDetailMapper;

	@Autowired
	private GoodsService goodsService;
	
	public void saleOrderBatchInsertTask() {
			long start = System.currentTimeMillis();
			System.out.println(Common.printTimeStamp() + "向T+系统批量增加销售订单任务开始了。。。。，开始中。。。");		
			try {			
				//读取全部待支付的订单列表
				Orders query = new Orders();
				query.setQueryUploadedTOrderStatus(Constants.TConstants.UPLOADED_TORDER_STATUS_A);
				List<Orders> orderList = orderService.selectByParam(query);
				for(int i = 0; i< orderList.size() ;i++){
					Orders order = orderList.get(i);
					try{
	
						//向T+系统查找该该订单是否已经添加过
						TSaleOrderResult  result = TOpenAPIUtil.getTSaleOrderByExternalCode(order.getOrderId(),false);
						if(result != null && result.getRows() != null && result.getRows().size() > 0){
							 List<TSaleOrder> saleOrderList = result.getRows();
							 for(TSaleOrder saleOrder:saleOrderList){
								 if(saleOrder.getExternalCode().equals(order.getOrderId())){
									 orderService.updateUploadTOrderStatus(order.getOrderId(), Constants.TConstants.UPLOADED_TORDER_STATUS_B);
									 break;
								 }
							 }
						}					
					    OrderShip orderShip = orderShipMapper.selectOrderShipByOrderId(order.getOrderId()) ;
						//向T+新增一个订单
						TSaleOrderInsertDto dto = new TSaleOrderInsertDto();
						TSaleOrderDto saleOrderDto = new TSaleOrderDto();
						saleOrderDto.setVoucherDate(order.getOrderCreatTime());
						saleOrderDto.setAddress(orderShip.getShipAddress());
						saleOrderDto.setContactPhone(orderShip.getTel());
						TCustomer customer = new TCustomer();
						customer.setCode(Constants.T_CUSTOMER_CODE);
						saleOrderDto.setCustomer(customer);
						Object[] dynamicPropertyKeys = {};
						saleOrderDto.setDynamicPropertyKeys(dynamicPropertyKeys);
						Object[] dynamicPropertyValues = {};
						saleOrderDto.setDynamicPropertyValues(dynamicPropertyValues);
						saleOrderDto.setExternalCode(order.getOrderId());
						saleOrderDto.setLinkMan(orderShip.getShipName());
						saleOrderDto.setMemo(order.getMemberId());
						saleOrderDto.setIsAutoAudit(false); //为True时，生成到T+中的销售订单自动进行审核，审核人与制单人相同，不判断审核权限为Flase时。不进行自动审核
						TDataSource dataSource = new TDataSource();
						dataSource.setCode(Constants.TdataSource.T_DATASOURCE_B2B);
						saleOrderDto.setDataSource(dataSource);
						List<TSaleOrderDetails> saleOrderDetails = new ArrayList<TSaleOrderDetails>();
						List<OrderDetail> orderDetails = orderDetailMapper.selectListByOrderId(order.getOrderId());
						com.sc.dal.model.order.Order o_ = orderService.selectOrderByOrderId(order.getOrderId());
						for(OrderDetail detail :orderDetails){
							TSaleOrderDetails orderDetail = new TSaleOrderDetails();
							Object[] dynamicPropertyKeys_ = {};
							Object[] dynamicPropertyValues_ = {};
							orderDetail.setDynamicPropertyKeys(dynamicPropertyKeys_);
							orderDetail.setDynamicPropertyValues(dynamicPropertyValues_);
							orderDetail.setOrigPrice(detail.getMarketPrice());
							orderDetail.setOrigTaxAmount(new BigDecimal(orderService.orderDetailProMoney(o_,detail,orderDetails.size()).get(detail.getId()).toString()));
							orderDetail.setIsPresent(false);//需要查询是否为赠品
							orderDetail.setQuantity(detail.getNumber());
							TInventory tInventory = new TInventory();
							Integer prdouctId = detail.getProductId();
							Goods goods =goodsService.selectByPrimaryKey(prdouctId);
							tInventory.setCode(goods.getCode());
							orderDetail.setInventory(tInventory);
							TUnit unit =new TUnit();
							unit.setName(goods.getUnitName());
							orderDetail.setUnit(unit);
							saleOrderDetails.add(orderDetail);
						}
						saleOrderDto.setSaleOrderDetails(saleOrderDetails);
						dto.setDto(saleOrderDto);
						TSaleOrderInsertResult saleOrderInsertResult = TOpenAPIUtil.insertOrderToTSaleOrder(dto,false);
						if(saleOrderInsertResult == null){
							 orderService.updateUploadTOrderStatus(order.getOrderId(), Constants.TConstants.UPLOADED_TORDER_STATUS_B);
						}
					}catch(Exception e){
						e.printStackTrace();
					}
				}
			} catch (Exception e) {    
				e.printStackTrace();
			}		
			
			long end = System.currentTimeMillis();
			long hj = end - start;
			System.out.println(Common.printTimeStamp() + "向T+系统批量增加销售订单任务结束了。。。" + "共计用了：" + hj / 1000 + "秒");

		
	//	}
		
	}
}
